package com.hcc.export.helper.core.event;

import com.hcc.export.helper.core.domain.ExportTask;
import com.hcc.export.helper.core.service.ExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;

/**
 * @author hushengjun
 * @since 2024/01/09 10:57:56
 */
@Slf4j
@Component
public class ExportTaskEventListener {

    @Autowired
    private ExecutorService executorService;

    @Autowired
    private ExportService exportService;

    @EventListener
    public void exportTaskEventListener(ExportTaskEvent event) {
        ExportTask exportTask = (ExportTask) event.getSource();
        Long recordId = exportTask.getRecordId();
        executorService.execute(() -> {
            try {
                exportService.export(recordId);
            } catch (Exception e) {
                log.error("导出异常", e);
            }
        });
    }

}
